﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_Asset
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<M_Asset> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_Asset> infoList = new List<M_Asset>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT asset_code,asset_name,asset_date,serial_number,ass.price,asset_remark,rc.icm_receive_code,rc.receive_date "
                        + " FROM icm_asset ass "
                        + " LEFT JOIN icm_receive rc ON ass.icm_receive_seq = rc.icm_receive_seq "
                        + " and rc.record_status <> 'D' "
                        + " WHERE ass.record_status<>'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_Asset info = new M_Asset();
                        info.asset_code = drc[i]["asset_code"].ToString();
                        info.asset_name = drc[i]["asset_name"].ToString();
                        info.asset_date = Convert.ToDateTime(drc[i]["asset_date"]);
                        info.serial_number = drc[i]["serial_number"].ToString();
                        info.asset_remark = drc[i]["asset_remark"].ToString();
                        info.price = Convert.ToDouble(drc[i]["price"].ToString());
                        info.asset_remark = drc[i]["asset_remark"].ToString();
                        info.icm_receive_code = drc[i]["icm_receive_code"].ToString();
                        info.receive_date = cn.ConvertNullDateTime(drc[i]["receive_date"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<M_Asset> GetListFillByName(string asset_name)
        {
            string strSQL = "";
            DataRowCollection drc = null;
            IList<M_Asset> infoList = new List<M_Asset>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT ass.asset_code,asset_name,asset_remark,ass.record_status,nr.repair_status "
                        + " FROM icm_asset ass "
                        + " LEFT JOIN icm_inventory_repair nr ON ass.asset_code = nr.asset_code "
                        + " WHERE ass.record_status <>'D' and (nr.asset_code is null OR nr.repair_status<>'R')  and asset_name LIKE '%' '" + asset_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_Asset info = new M_Asset();
                        info.asset_code = drc[i]["asset_code"].ToString();
                        info.asset_name = drc[i]["asset_name"].ToString();
                        info.asset_remark = drc[i]["asset_remark"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }

        public M_Asset GetListFillBySeq(string asset_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_Asset info = new M_Asset();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = " SELECT asset_code,asset_name,asset_date,serial_number,ass.price,asset_remark,rc.icm_receive_code,rc.receive_date "
                        + " FROM ICM_ASSET ass "
                        + " LEFT JOIN ICM_RECEIVE rc ON ass.icm_receive_seq = rc.icm_receive_seq "
                        + " and rc.record_status <> @DeleteRecord "
                        + " WHERE ass.record_status<> @DeleteRecord "
                            + " and asset_code LIKE '%' '" + asset_code + "'  '%' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.asset_code = drc[0]["asset_code"].ToString();
                    info.asset_name = drc[0]["asset_name"].ToString();
                    info.asset_date = Convert.ToDateTime(drc[0]["asset_date"]);
                    info.serial_number = drc[0]["serial_number"].ToString();
                    info.asset_remark = drc[0]["asset_remark"].ToString();
                    info.price = Convert.ToDouble(drc[0]["price"].ToString());
                    info.asset_remark = drc[0]["asset_remark"].ToString();
                    info.icm_receive_code = drc[0]["icm_receive_code"].ToString();
                    info.receive_date = cn.ConvertNullDateTime(drc[0]["receive_date"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public M_Asset GenerateAssetCode(string asset_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_Asset info = new M_Asset();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@asset_code", asset_code);

                //strSQL = " SELECT case when  (Year(Now()) <2500) then Year(Now())+543 else Year(Now()) end YearValue , Month(Now()) as MonthValue  ,COUNT(asset_code)+1 as running "
                //         +" FROM icm_asset asset "
                //         +" WHERE YEAR(asset_date) = YEAR(Now()) and "
                //         +" Month(asset_date) = Month(Now())  ";
                strSQL = " SELECT case when  (Year(Now()) <2500) then Year(Now())+543 else Year(Now()) end YearValue , Month(Now()) as MonthValue  "
                        + " ,COUNT(asset_code)+1 as running "
                        + " FROM icm_asset asset  "
                        + " WHERE YEAR(created_when) = YEAR(Now()) and  Month(created_when) = Month(Now())  and (right(left(asset_code,19),12) = @asset_code) ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.stryear = drc[0]["YearValue"].ToString();
                    info.strmonth = drc[0]["MonthValue"].ToString();
                    info.assetcode_running = drc[0]["running"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public M_Asset GenerateRunningNumber(string sub_asset_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_Asset info = new M_Asset();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@sub_asset_code", sub_asset_code);

                strSQL = " SELECT count(asset_code)+1 as running  "
                        + " FROM icm_asset "
                        + " WHERE record_status <>'D' "
                        + " and left(asset_code,19) =@sub_asset_code  ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.assetcode_running = drc[0]["running"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert_Asset(M_Asset info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(12);
                dbManager.AddParameters(0, "@asset_code", info.asset_code);
                dbManager.AddParameters(1, "@asset_name", info.asset_name);
                dbManager.AddParameters(2, "@asset_date", info.asset_date);
                dbManager.AddParameters(3, "@serial_number", info.serial_number);
                dbManager.AddParameters(4, "@asset_remark", info.asset_remark);
                dbManager.AddParameters(5, "@depreciate_below_flag", info.depreciate_below_flag);
                dbManager.AddParameters(6, "@price", info.price);
                dbManager.AddParameters(7, "@icm_res_item_seq", info.icm_res_item_seq);
                dbManager.AddParameters(8, "@record_status", info.record_status);
                dbManager.AddParameters(9, "@created_by", info.created_by);
                dbManager.AddParameters(10, "@created_when", info.created_when);
                dbManager.AddParameters(11, "@icm_receive_seq", info.icm_receive_seq);

                strSQL = " INSERT INTO icm_asset "
                        + " (asset_code,asset_name,asset_date,serial_number,asset_remark "
                        + " ,depreciate_below_flag,price,icm_res_item_seq,record_status "
                        + " ,created_by,created_when,icm_receive_seq) "
                        + " VALUES "
                        + " (@asset_code,@asset_name,@asset_date,@serial_number,@asset_remark "
                        + " ,@depreciate_below_flag,@price,@icm_res_item_seq,@record_status "
                        + " ,@created_by,@created_when,icm_receive_seq) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update_Asset(M_Asset info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(11);
                dbManager.AddParameters(0, "@asset_code", info.asset_code);
                dbManager.AddParameters(1, "@asset_name", info.asset_name);
                dbManager.AddParameters(2, "@asset_date", info.asset_date);
                dbManager.AddParameters(3, "@serial_number", info.serial_number);
                dbManager.AddParameters(4, "@asset_remark", info.asset_remark);
                dbManager.AddParameters(5, "@depreciate_below_flag", info.depreciate_below_flag);
                dbManager.AddParameters(6, "@price", info.price);
                dbManager.AddParameters(7, "@icm_res_item_seq", info.icm_res_item_seq);
                dbManager.AddParameters(8, "@record_status", info.record_status);
                dbManager.AddParameters(9, "@updated_by", info.updated_by);
                dbManager.AddParameters(10, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_asset "
                        + " SET asset_name = @asset_name "
                        + " ,asset_date = @asset_date "
                        + " ,serial_number = @serial_number "
                        + " ,asset_remark =@asset_remark "
                        + " ,depreciate_below_flag = @depreciate_below_flag "
                        + " ,price = @price "
                        + " ,icm_res_item_seq = @icm_res_item_seq "
                        + " ,record_status = @record_status "
                        + " ,updated_by =@updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE asset_code = @asset_code ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }


        public void Update_Asset_ICM_RECEIVE(M_IcmReceiveObject info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@asset_code_start", info.asset_code_start);
                dbManager.AddParameters(1, "@asset_code_finish", info.asset_code_finish);
                dbManager.AddParameters(2, "@icm_receive_seq", info.icm_receive_seq);

                strSQL = " UPDATE icm_asset "
                        + " SET icm_receive_seq = @icm_receive_seq "
                        + " WHERE asset_code >=  '@asset_code_start' AND "
                        + " asset_code <= '@asset_code_finish' ";
                //'+@asset_code_start+' "
                //        + " AND asset_code <='@asset_code_finish' ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }


        public void Delete_Asset(string asset_code_start, string asset_code_finish, string remark)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@asset_code_start", asset_code_start);
                dbManager.AddParameters(1, "@asset_code_finish", asset_code_finish);
                dbManager.AddParameters(2, "@remark", remark);

                strSQL = " UPDATE icm_asset "
                        + " SET record_status = 'D' "
                        + " ,asset_remark = @remark "
                        + " WHERE asset_code >=  '@asset_code_start' AND "
                        + " asset_code <= '@asset_code_finish' ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

    }
}
